package com.oceansoft.mobile.econsole.modules.sys.service.impl;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oceansoft.mobile.econsole.cache.redis.CacheBaseService;
import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.common.util.web.PageUtil;
import com.oceansoft.mobile.econsole.modules.advice.entity.Advice;
import com.oceansoft.mobile.econsole.modules.base.entity.Pager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.message.entity.PushMessage;
import com.oceansoft.mobile.econsole.modules.sys.entity.ApiInfo;
import com.oceansoft.mobile.econsole.modules.sys.entity.ApiMonitorLog;
import com.oceansoft.mobile.econsole.modules.sys.entity.ApiMonitorSMS;
import com.oceansoft.mobile.econsole.modules.sys.entity.ParamInfo;
import com.oceansoft.mobile.econsole.modules.sys.entity.Record;
import com.oceansoft.mobile.econsole.modules.sys.persistence.IApiInfoMapper;
import com.oceansoft.mobile.econsole.modules.sys.service.IApiInfoService;

@Service
public class ApiInfoServiceImpl extends CacheBaseService implements
		IApiInfoService {

	@Resource
	private IApiInfoMapper apiInfoMapper;

	@Override
	public Pager<ApiInfo> findByPage(Query query) {
		PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
		return PageUtil
				.page2pager((Page<ApiInfo>) apiInfoMapper
						.loadPageByCondition(query), query);
	}

	@Override
	@Transactional
	public Result save(ApiInfo apiInfo) {
		int rt = apiInfoMapper.insert(apiInfo);
		if (rt > 0) {

			return new Result(apiInfo);
		} else {
			return new Result(Result.FAILURE, "新增失败");
		}
	}

	@Override
	@Transactional
	public Result delete(int apiId) {
		apiInfoMapper.delete(apiId);
		return new Result();
	}

	@Override
	public ApiInfo findApiBaseInfoById(int apiId) {
		ApiInfo apiInfo = apiInfoMapper.findApiBaseInfoById(apiId);
		// TODO Auto-generated method stub
		return apiInfo;
	}

	@Override
	@Transactional
	public Result updateApiInfo(ApiInfo apiInfo) {
		apiInfoMapper.updateApiInfo(apiInfo);
		return new Result();
	}

	@Override
	public int countParam(int apiId) {

		int count = apiInfoMapper.countParam(apiId);
		return count;
	}

	@Override
	public ParamInfo findParamByApiId(int apiId) {
		ParamInfo param = apiInfoMapper.findParamById(apiId);
		return param;
	}

	@Override
	public void saveParam(ParamInfo param) {

		apiInfoMapper.saveParam(param);
	}
	@Override
	public Result submitALLAPiInfo(String url, Map<String, String> map,
			String string) {
			String rt=null;
			rt=HttpClientUtil.doPost(url, map, "UTF-8");
			//HttpClientUtil.doGAUrlPost(url, allParamInfo);
			if (rt == null) {
				rt = "接口异常";
			} else {
				// 响应正常容错为0
				rt="接口正常";
			}
			return new Result(rt);
		
		//return new Result(Result.SUCC,"接口正常");
	}

	@Override
	public Pager<Record> findRecordInfoByPage(Query query) {
		 PageHelper.startPage(query.getPageNum(), query.getNumPerPage(), true);
		 Page<Record> page = (Page<Record>) apiInfoMapper.loadRecordPageByCondition(query);
		 return PageUtil.page2pager(page, query);
	}
	@Override
	public Result submitGetAPiInfo(String url) {
		try {
			HttpClientUtil.doGet(url);
		} catch (IllegalArgumentException e) {
			return new Result(Result.FAILURE,"接口异常");
		} catch (IOException e) {
			return new Result(Result.FAILURE,"接口异常");
		}
		return new Result(Result.SUCC,"接口正常");
	}
	@Override
	public Result updateAPIStatus(int apiId,int status) {
		apiInfoMapper.updateAPIStatus(apiId,status);
	    return new Result(Result.SUCC,"更新成功");
	}
	
	// @Override
	// public void deleteParam(int apiId) {
	// apiInfoMapper.deleteParam(apiId);
	//
	// }
	@Override
	public List<ApiInfo> findAllApiInfo() {
		return apiInfoMapper.findAllApiInfo();
	}
	@Override
	@Transactional
	public void saveApiMonitorLog(ApiMonitorLog log) {
		apiInfoMapper.saveApiMonitorLog(log);
	}

	@Override
	@Transactional
	public void saveApiMonitorSMS(ApiMonitorSMS sms) {
		apiInfoMapper.saveApiMonitorSMS(sms);
	}

	@Override
	public int alreadyHasSMS(ApiInfo apiInfo) {
		return apiInfoMapper.getSMSByTime(apiInfo);
	}

	

	
}
